home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / boot / initrd.img-2.6.20-15-generic.bak / initrd.img-2.6.20-15-generic / scripts / casper < prev    next >
Encoding:
Text File  |  2007-05-18  |  16.1 KB  |  550 lines

  1. #!/bin/sh
  2.  
  3. # set -e
  4.  
  5. export PATH=/root/usr/bin:/root/usr/sbin:/root/bin:/root/sbin:/usr/bin:/usr/sbin:/bin:/sbin
  6.  
  7. echo "/root/lib" >> /etc/ld.so.conf
  8. echo "/root/usr/lib" >> /etc/ld.so.conf
  9.  
  10. mountpoint=/cdrom
  11.  
  12. USERNAME=casper
  13. USERFULLNAME="Live session user"
  14. HOST=live
  15. BUILD_SYSTEM=Custom
  16.  
  17. mkdir -p $mountpoint
  18.  
  19. [ -f /etc/casper.conf ] && . /etc/casper.conf
  20. export USERNAME USERFULLNAME HOST BUILD_SYSTEM
  21.  
  22. . /scripts/casper-helpers
  23.  
  24. if [ ! -f /casper.vars ]; then
  25.     touch /casper.vars
  26. fi
  27.  
  28. is_casper_path() {
  29.     path=$1
  30.     if [ -d "$path/casper" ]; then
  31.         if [ "$(echo $path/casper/*.squashfs)" != "$path/casper/*.squashfs" ] ||
  32.             [ "$(echo $path/casper/*.ext2)" != "$path/casper/*.ext2" ] ||
  33.             [ "$(echo $path/casper/*.dir)" != "$path/casper/*.dir" ]; then
  34.             return 0
  35.         fi
  36.     fi
  37.     return 1
  38. }
  39.  
  40. get_backing_device() {
  41.     case "$1" in
  42.         *.squashfs|*.ext2)
  43.             echo $(setup_loop "$1" "loop" "/sys/block/loop*")
  44.             ;;
  45.         *.dir)
  46.             echo "directory"
  47.             ;;
  48.         *)
  49.             panic "Unrecognized casper filesystem: $1"
  50.             ;;
  51.     esac
  52. }
  53.  
  54. match_files_in_dir() {
  55.     # Does any files match pattern $1 ?
  56.  
  57.     local pattern="$1"
  58.     if [ "$(echo $pattern)" != "$pattern" ]; then
  59.         return 0
  60.     fi
  61.     return 1
  62. }
  63.  
  64. mount_images_in_directory() {
  65.     directory="$1"
  66.     rootmnt="$2"
  67.     if match_files_in_dir "$directory/casper/*.squashfs" ||
  68.         match_files_in_dir "$directory/casper/*.ext2" ||
  69.         match_files_in_dir "$directory/casper/*.dir"; then
  70.         setup_unionfs "$directory/casper" "$rootmnt"
  71.     else
  72.         :
  73.     fi
  74. }
  75.  
  76. is_nice_device() {
  77.     sysfs_path="${1#/sys}"
  78.     if /lib/udev/path_id "${sysfs_path}" | grep -E -q "ID_PATH=(usb|pci-[^-]*-[ide|scsi|usb])"; then
  79.         return 0
  80.     fi
  81.     return 1
  82. }
  83.  
  84. is_supported_fs () {
  85.     # FIXME: do something better like the scan of supported filesystems
  86.     fstype="${1}"
  87.     case ${fstype} in
  88.         vfat|iso9660|udf|ext2|ext3|ntfs)
  89.             return 0
  90.             ;;
  91.     esac
  92.     return 1
  93. }
  94.  
  95. copy_live_to() {
  96.     copyfrom="${1}"
  97.     copytodev="${2}"
  98.     copyto="${copyfrom}_swap"
  99.  
  100.     size=$(fs_size "" ${copyfrom} "used")
  101.  
  102.     if [ "${copytodev}" = "ram" ]; then
  103.         # copying to ram:
  104.         freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ) )
  105.         mount_options="-o size=${size}k"
  106.         free_string="memory"
  107.         fstype="tmpfs"
  108.         dev="/dev/shm"
  109.     else
  110.         # it should be a writable block device
  111.         if [ -b "${copytodev}" ]; then
  112.             dev="${copytodev}"
  113.             free_string="space"
  114.             fstype=$(get_fstype "${dev}")
  115.             freespace=$(fs_size "${dev}")
  116.         else
  117.             [ "$quiet" != "y" ] && log_warning_msg "${copytodev} is not a block device."
  118.             return 1
  119.         fi
  120.     fi
  121.     if [ "${freespace}" -lt "${size}" ] ; then
  122.         [ "$quiet" != "y" ] && log_warning_msg "Not enough free ${free_string} (${freespace}k > ${size}k) to copy live media in ${copytodev}."
  123.         return 1
  124.     fi
  125.  
  126.     # begin copying..
  127.     mkdir "${copyto}"
  128.     echo "mount -t ${fstype} ${mount_options} ${dev} ${copyto}"
  129.     mount -t "${fstype}" ${mount_options} "${dev}" "${copyto}"
  130.     cp -a ${copyfrom}/* ${copyto} # "cp -a" from busybox also copies hidden files
  131.     umount ${copyfrom}
  132.     mount -r -o move ${copyto} ${copyfrom}
  133.     rmdir ${copyto}
  134.     return 0
  135. }
  136.  
  137. do_netmount() {
  138.     rc=1
  139.  
  140.     modprobe "${MP_QUIET}" af_packet # For DHCP
  141.  
  142.     udevtrigger
  143.     udevsettle
  144.  
  145.     ipconfig ${DEVICE} /tmp/net-${DEVICE}.conf | tee /netboot.config
  146.  
  147.     if [ "${NFSROOT}" = "auto" ]; then
  148.         NFSROOT=${ROOTSERVER}:${ROOTPATH}
  149.     fi
  150.  
  151.     [ "$quiet" != "y" ] && log_begin_msg "Trying netboot from ${NFSROOT}"
  152.  
  153.     if [ "${NETBOOT}" != "nfs" ] && do_cifsmount ; then
  154.         rc=0
  155.     elif do_nfsmount ; then
  156.         NETBOOT="nfs"
  157.         export NETBOOT
  158.         rc=0
  159.     fi
  160.  
  161.     [ "$quiet" != "y" ] && log_end_msg
  162.     return ${rc}
  163. }
  164.  
  165. do_nfsmount() {
  166.     rc=1
  167.     modprobe "${MP_QUIET}" nfs
  168.     if [ -z "${NFSOPTS}" ]; then
  169.         NFSOPTS=""
  170.     fi
  171.  
  172.     [ "$quiet" != "y" ] && log_begin_msg "Trying nfsmount -o nolock -o ro ${NFSOPTS} ${NFSROOT} ${mountpoint}"
  173.     # FIXME: This for loop is an ugly HACK round an nfs bug
  174.     for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
  175.         nfsmount -o nolock -o ro ${NFSOPTS} "${NFSROOT}" "${mountpoint}" && rc=0 && break
  176.         sleep 1
  177.     done
  178.     return ${rc}
  179. }
  180.  
  181. do_cifsmount() {
  182.     rc=1
  183.     if [ -x "/sbin/mount.cifs" ]; then
  184.         if [ -z "${NFSOPTS}" ]; then
  185.             CIFSOPTS="-ouser=root,password="
  186.         else
  187.             CIFSOPTS="${NFSOPTS}"
  188.         fi
  189.  
  190.         [ "$quiet" != "y" ] && log_begin_msg "Trying mount.cifs ${NFSROOT} ${mountpoint} ${CIFSOPTS}"
  191.         modprobe "${MP_QUIET}" cifs
  192.  
  193.         if mount.cifs "${NFSROOT}" "${mountpoint}" "${CIFSOPTS}" ; then
  194.             rc=0
  195.         fi
  196.     fi
  197.     return ${rc}
  198. }
  199.  
  200. do_snap_copy ()
  201. {
  202.     fromdev="${1}"
  203.     todir="${2}"
  204.     snap_type="${3}"
  205.  
  206.     size=$(fs_size "${fromdev}" "" "used")
  207.  
  208.     if [ -b "${fromdev}" ]; then
  209.         # look for free mem
  210.         if [ -n "${HOMEMOUNTED}" -a "${snap_type}" = "HOME" ]; then
  211.             todev=$(cat /proc/mounts | grep -s " $(base_path ${todir}) " | awk '{print $1}' )
  212.             freespace=$(df -k  | grep -s ${todev} | awk '{print $4}')
  213.         else
  214.             freespace=$( expr $(awk '/MemFree/{print $2}' /proc/meminfo) + $( cat /proc/meminfo | grep Cached | head -n 1 | awk '/Cached/{print $2}' - ))
  215.         fi
  216.  
  217.         tomount="/mnt/tmpsnap"
  218.         if [ ! -d "${tomount}" ] ; then
  219.             mkdir -p "${tomount}"
  220.         fi
  221.  
  222.         fstype=$(get_fstype "${fromdev}")
  223.         if [ -n "${fstype}" ]; then
  224.             # Copying stuff...
  225.             mount -t "${fstype}" -o ro "${fromdev}" "${tomount}"
  226.             cp -a "${tomount}"/* ${todir}
  227.             umount "${tomount}"
  228.         else
  229.             log_warning_msg "Unrecognized fstype: ${fstype} on ${fromdev}:${snap_type}"
  230.         fi
  231.  
  232.         rmdir "${tomount}"
  233.         if echo ${fromdev} | grep -qs loop; then
  234.            losetup -d "${fromdev}"
  235.         fi
  236.         return 0
  237.     else
  238.         return 1
  239.         [ "$quiet" != "y" ] && log_warning_msg "Unable to find the snapshot ${snap_type} medium"
  240.     fi
  241. }
  242.  
  243. try_snap ()
  244. {
  245.     # Look for $snap_label.* in block devices and copy the contents to $snap_mount
  246.     #   and remember the device and filename for resync on exit in casper.init
  247.  
  248.     snap_label="${1}"
  249.     snap_mount="${2}"
  250.     snap_type="${3}"
  251.  
  252.     snapdata=$(find_files "${snap_label}.squashfs ${snap_label}.cpio.gz ${snap_label}.ext2")
  253.     if [ ! -z "${snapdata}" ]; then
  254.         snapdev="$(echo ${snapdata} | cut -f1 -d ' ')"
  255.         snapback="$(echo ${snapdata} | cut -f2 -d ' ')"
  256.         snapfile="$(echo ${snapdata} | cut -f3 -d ' ')"
  257.         if echo "${snapfile}" | grep -qs '\(squashfs\|ext2\)'; then
  258.             # squashfs or ext2 snapshot
  259.             dev=$(get_backing_device "${snapback}/${snapfile}")
  260.             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}"; then
  261.                  log_warning_msg "Impossible to include the ${snapfile} Snapshot"
  262.                  return 1
  263.             fi
  264.         else
  265.             # cpio.gz snapshot
  266.             if ! (cd "${snap_mount}" && zcat "${snapback}/${snapfile}" | cpio -i -u -d 2>/dev/null) ; then
  267.                 log_warning_msg "Impossible to include the ${snapfile} Snapshot"
  268.                 return 1
  269.             fi
  270.         fi
  271.         umount "${snapback}"
  272.     else
  273.         dev=$(find_cow_device "${snap_label}")
  274.         if [ -b ${dev} ]; then
  275.             if echo "${dev}" | grep -qs loop; then
  276.                 # strange things happens, user confused?
  277.                 snaploop=$( losetup ${dev} | awk '{print $3}' | tr -d '()' )
  278.                 snapfile=$(basename ${snaploop})
  279.                 snapdev=$(cat /proc/mounts | awk '{print $2,$1}' | grep -es "^$( dirname ${snaploop} )" | cut -f2 -d ' ')
  280.             else
  281.                 snapdev="${dev}"
  282.             fi
  283.             if ! do_snap_copy "${dev}" "${snap_mount}" "${snap_type}" ; then
  284.                 log_warning_msg "Impossible to include the ${snap_label} Snapshot"
  285.                 return 1
  286.             else
  287.                 if [ -n "${snapfile}" ]; then
  288.                      # it was a loop device, user confused
  289.                      umount ${snapdev}
  290.                 fi
  291.             fi
  292.         else
  293.             log_warning_msg "Impossible to include the ${snap_label} Snapshot"
  294.             return 1
  295.         fi
  296.     fi
  297.     echo "export ${snap_type}SNAP="${snap_mount}":${snapdev}:${snapfile}" >> /etc/casper.conf # for resync on reboot/halt
  298.     return 0
  299. }
  300.  
  301. setup_unionfs() {
  302.     image_directory="$1"
  303.     rootmnt="$2"
  304.  
  305.     modprobe "${MP_QUIET}" -b unionfs
  306.  
  307.     # run-init can't deal with images in a subdir, but we're going to
  308.     # move all of these away before it runs anyway.  No, we're not,
  309.     # put them in / since move-mounting them into / breaks mono and
  310.     # some other apps.
  311.  
  312.     croot="/"
  313.  
  314.     # Let's just mount the read-only file systems first
  315.     rofsstring=""
  316.     rofslist=""
  317.     if [ "${NETBOOT}" = "nfs" ] ; then
  318.         roopt="nfsro" # go aroung a bug in nfs-unionfs locking
  319.     else
  320.         roopt="ro"
  321.     fi
  322.  
  323.     mkdir -p "${croot}"
  324.     for image_type in "ext2" "squashfs" "dir" ; do
  325.         for image in "${image_directory}"/*."${image_type}"; do
  326.             imagename=$(basename "${image}")
  327.             if [ -d "${image}" ]; then
  328.                 # it is a plain directory: do nothing
  329.                 rofsstring="${image}=${roopt}:${rofsstring}"
  330.                 rofslist="${image} ${rofslist}"
  331.             elif [ -f "${image}" ]; then
  332.                 backdev=$(get_backing_device "$image")
  333.                 fstype=$(get_fstype "${backdev}")
  334.                 if [ "${fstype}" = "unknown" ]; then
  335.                     panic "Unknown file system type on ${backdev} (${image})"
  336.                 fi
  337.                 mkdir -p "${croot}/${imagename}"
  338.                 mount -t "${fstype}" -o ro "${backdev}" "${croot}/${imagename}" || panic "Can not mount $backdev ($image) on ${croot}/${imagename}" && rofsstring="${croot}/${imagename}=${roopt}:${rofsstring}" && rofslist="${croot}/${imagename} ${rofslist}"
  339.             fi
  340.         done
  341.     done
  342.     rofsstring=${rofsstring%:}
  343.  
  344.     mkdir -p /cow
  345.     cowdevice="tmpfs"
  346.     cow_fstype="tmpfs"
  347.  
  348.     # Looking for "${root_persistence}" device or file
  349.     if [ -n "${PERSISTENT}" ]; then
  350.         cowprobe=$(find_cow_device "${root_persistence}")
  351.         if [ -b "${cowprobe}" ]; then
  352.             cowdevice=${cowprobe}
  353.             cow_fstype=$(get_fstype "${cowprobe}")
  354.         else
  355.             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent medium"
  356.         fi
  357.     fi
  358.  
  359.     mount ${cowdevice} -t ${cow_fstype} -o rw /cow || panic "Can not mount $cowdevice on /cow"
  360.  
  361.     mount -t unionfs -o dirs=/cow=rw:$rofsstring unionfs "$rootmnt" || panic "Unionfs mount failed"
  362.  
  363.     # Adding other custom mounts
  364.     if [ -n "${PERSISTENT}" ]; then
  365.         # directly mount /home
  366.         # FIXME: add a custom mounts configurable system
  367.         homecow=$(find_cow_device "${home_persistence}" )
  368.         if [ -b "${homecow}" ]; then
  369.             mount -t $(get_fstype "${homecow}") -o rw "${homecow}" "${rootmnt}/home"
  370.             export HOMEMOUNTED=1 # used to proper calculate free space in do_snap_copy()
  371.         else
  372.             [ "$quiet" != "y" ] && log_warning_msg "Unable to find the persistent home medium"
  373.         fi
  374.         # Look for other snapshots to copy in
  375.         try_snap "${root_snapshot_label}" "${rootmnt}" "ROOT"
  376.         try_snap "${home_snapshot_label}" "${rootmnt}/home" "HOME"
  377.     fi
  378.  
  379.     if [ -n "${SHOWMOUNTS}" ]; then
  380.         for d in ${rofslist}; do
  381.             mkdir -p "${rootmnt}/casper/${d##*/}"
  382.             case d in
  383.                 *.dir) # do nothing # mount -o bind "${d}" "${rootmnt}/casper/${d##*/}"
  384.                     ;;
  385.                 *) mount -o move "${d}" "${rootmnt}/casper/${d##*/}"
  386.                     ;;
  387.             esac
  388.         done
  389.         # shows cow fs on /cow for use by casper-snapshot
  390.         mkdir -p "${rootmnt}/cow"
  391.         mount -o bind /cow "${rootmnt}/cow"
  392.     fi
  393.  
  394.     # move the first mount; no head in busybox-initramfs
  395.     for d in $(mount -t squashfs | cut -d\  -f 3); do
  396.         mkdir -p "${rootmnt}/rofs"
  397.         mount -o move "${d}" "${rootmnt}/rofs"
  398.         break
  399.     done
  400. }
  401.  
  402. check_dev ()
  403. {
  404.     sysdev="${1}"
  405.     devname="${2}"
  406.     if [ -z "${devname}" ]; then
  407.         devname=$(sys2dev "${sysdev}")
  408.     fi
  409.  
  410.     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
  411.         loopdevname=$(setup_loop "${devname}" "loop" "/sys/block/loop*" "${LIVEMEDIA_OFFSET}")
  412.         devname="${loopdevname}" 
  413.     fi
  414.  
  415.     fstype=$(get_fstype "${devname}")
  416.     if is_supported_fs ${fstype}; then
  417.         mount -t ${fstype} -o ro "${devname}" $mountpoint || continue
  418.         if is_casper_path $mountpoint; then
  419.             echo $mountpoint
  420.             return 0
  421.         else
  422.             umount $mountpoint
  423.         fi
  424.     fi
  425.  
  426.     if [ -n "${LIVEMEDIA_OFFSET}" ]; then
  427.         losetup -d "${loopdevname}"
  428.     fi
  429.     return 1
  430. }
  431.  
  432. find_livefs() {
  433.     timeout="${1}"
  434.     # first look at the one specified in the command line
  435.     if [ ! -z "${LIVEMEDIA}" ]; then
  436.         if check_dev "null" "${LIVEMEDIA}"; then
  437.             return 0
  438.         fi
  439.     fi
  440.     # don't start autodetection before timeout has expired
  441.     if [ -n "${LIVEMEDIA_TIMEOUT}" ]; then
  442.         if [ "${timeout}" -lt "${LIVEMEDIA_TIMEOUT}" ]; then
  443.             return 1
  444.         fi
  445.     fi
  446.     # or do the scan of block devices
  447.     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop | grep -v ram); do
  448.         devname=$(sys2dev "${sysblock}")
  449.         fstype=$(get_fstype "${devname}")
  450.         if /lib/udev/cdrom_id ${devname} > /dev/null; then
  451.             if check_dev "null" "${devname}" ; then
  452.                 return 0
  453.             fi
  454.         elif is_nice_device "${sysblock}" ; then
  455.             for dev in $(subdevices "${sysblock}"); do
  456.                 if check_dev "${dev}" ; then
  457.                     return 0
  458.                 fi
  459.             done
  460.         elif [ "${fstype}" = "squashfs" -o \
  461.                 "${fstype}" = "ext3" -o \
  462.                 "${fstype}" = "ext2" ]; then
  463.             # This is an ugly hack situation, the block device has
  464.             # an image directly on it.  It's hopefully
  465.             # casper, so take it and run with it.
  466.             ln -s "${devname}" "${devname}.${fstype}"
  467.             echo "${devname}.${fstype}"
  468.             return 0
  469.         fi
  470.     done
  471.     return 1
  472. }
  473.  
  474. pulsate() {
  475.     if [ -x /sbin/usplash_write ]; then
  476.         /sbin/usplash_write "PULSATE"
  477.     fi
  478. }
  479.  
  480. set_usplash_timeout() {
  481.     if [ -x /sbin/usplash_write ]; then
  482.         /sbin/usplash_write "TIMEOUT 120"
  483.     fi
  484. }
  485.  
  486. mountroot() {
  487.     exec 6>&1
  488.     exec 7>&2
  489.     exec > casper.log
  490.     exec 2>&1
  491.  
  492.     set_usplash_timeout
  493.     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-premount"
  494.     pulsate
  495.     run_scripts /scripts/casper-premount
  496.     [ "$quiet" != "y" ] && log_end_msg
  497.  
  498.     # Needed here too because some things (*cough* udev *cough*)
  499.     # changes the timeout
  500.  
  501.     set_usplash_timeout
  502.  
  503.     if [ ! -z "${NETBOOT}" ]; then
  504.         if do_netmount ; then
  505.             livefs_root="${mountpoint}"
  506.         else
  507.             panic "Unable to find a live file system on the network"
  508.         fi
  509.     else
  510.         # Scan local devices for the image
  511.         for i in 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13; do
  512.             livefs_root=$(find_livefs $i)
  513.             if [ "${livefs_root}" ]; then
  514.                 break
  515.             fi
  516.             sleep 1
  517.         done
  518.     fi
  519.  
  520.     if [ -z "${livefs_root}" ]; then
  521.         panic "Unable to find a medium containing a live file system"
  522.     fi
  523.  
  524.     if [ "${TORAM}" ]; then
  525.         live_dest="ram"
  526.     elif [ "${TODISK}" ]; then
  527.         live_dest="${TODISK}"
  528.     fi
  529.     if [ "${live_dest}" ]; then
  530.         log_begin_msg "Copying live_media to ${live_dest}"
  531.         copy_live_to "${livefs_root}" "${live_dest}"
  532.         log_end_msg
  533.     fi
  534.  
  535.     mount_images_in_directory "${livefs_root}" "${rootmnt}"
  536.  
  537.     log_end_msg
  538.  
  539.     maybe_break casper-bottom
  540.     [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/casper-bottom"
  541.  
  542.     pulsate
  543.     run_scripts /scripts/casper-bottom
  544.     [ "$quiet" != "y" ] && log_end_msg
  545.  
  546.     exec 1>&6 6>&-
  547.     exec 2>&7 7>&-
  548.     cp casper.log "${rootmnt}/var/log/"
  549. }
  550.